package com.zzq.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzq.dao.AddressMapper;
import com.zzq.entity.PageResult;
import com.zzq.entity.QueryPageBean;
import com.zzq.pojo.Address;
import com.zzq.pojo.AddressExample;
import com.zzq.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 幼儿猿大班
 * 2021/7/30
 */
@Service(interfaceClass = AddressService.class)
public class AddressServiceImpl implements AddressService {


    @Autowired
    private AddressMapper addressMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        AddressExample addressExample = new AddressExample();
        if (queryPageBean.getQueryString()!=null&&!queryPageBean.getQueryString().equals("")) {
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andAddressnameLike("%"+queryPageBean.getQueryString()+"%");
        }
        List<Address> addressList = addressMapper.selectByExample(addressExample);
        PageInfo<Address> addressPageInfo = new PageInfo<>(addressList);
        return new PageResult(addressPageInfo.getTotal(),addressPageInfo.getList());
    }

    @Override
    public void deleteById(Integer id) {
        addressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void address(Map<String, Object> map) {
        addressMapper.insert(new Address(null,map.get("addressName").toString(),map.get("lng").toString(),map.get("lat").toString()));
    }

    @Override
    public Map<String, Object> findMaps() {
        HashMap<String, Object> hashMap2 = new HashMap<>();


        List<Map> gridnMaps=new ArrayList<>();
        List<Map> nameMaps=new ArrayList<>();

        List<Address> addressList = addressMapper.selectByExample(null);

        for (Address address : addressList) {
            HashMap<String, String> hashMap = new HashMap<>();
            HashMap<String, String> hashMap1 = new HashMap<>();
            hashMap1.put("addressName",address.getAddressName());

            hashMap.put("lng",address.getLng());
            hashMap.put("lat",address.getLat());

            gridnMaps.add(hashMap);
            nameMaps.add(hashMap1);
        }
       hashMap2.put("nameMaps",nameMaps);
        hashMap2.put("gridnMaps",gridnMaps);
        return hashMap2;
    }


}
